<?php

include('IXR_Library.inc.php');

/* Functions defining the behaviour of the server */


/**************************************
*Ultimos jogos
*
***************************************/
function getGames($args) {

    $con = mysql_connect("localhost","barreirasacs","serverb");

	if (!$con)
	{
		$out = "erro";
	}else{

		mysql_select_db("acscore", $con);
		
		$result = mysql_query("SELECT * FROM game order by id desc limit 0,5");

              $out='<table  border="1" cellpadding="2" cellspacing="2">';
		$out.='<thead>';
		$out.='<tr>';
		$out.='<th>MAP</th>';
		$out.='<th>GAME STARTED</th>';
		$out.='<th>ENDED</th>';
		$out.='</tr>';
		$out.='</thead>';
		$out.='<tbody>';

		$i=0;
		while($row = mysql_fetch_array($result))
		{
			if ($i % 2) {
				$odeven="even";
			} else {
				$odeven="odd";
			}

			$i++;
		  	
			$out.='<tr>';
			$out.='<td class="'.$odeven.'" >' . $row['map'] . ' </td>';
			$out.='<td class="'.$odeven.'">' . $row['started'] .'</td>';
			$out.='<td class="'.$odeven.'">' . $row['ended'] . '</td>';
			$out.='</tr>';
			
		}
		
		$out.='</tbody>';
		$out.='</table>';
	}

	return $out;
}

/**************************************
*Player frags
*
***************************************/
function getFraggers($args) {

    $con = mysql_connect("localhost","barreirasacs","serverb");

	if (!$con)
	{
		$out="erro";
	}else{

		mysql_select_db("acscore", $con);
		
		$result = mysql_query("
SELECT fragger, count(fragger) as frags FROM `gamefrag` WHERE 1
group by fragger
order by count(fragger) desc
limit 5

");

              $out='<table  border="1" cellpadding="2" cellspacing="2">';
		$out.='<thead>';
		$out.='<tr>';
		$out.='<th>PLAYER</th>';
		$out.='<th>TOTAL FRAGS</th>';
		$out.='</tr>';
		$out.='</thead>';
		$out.='<tbody>';

		$i=0;
		while($row = mysql_fetch_array($result))
		{
			if ($i % 2) {
				$odeven="even";
			} else {
				$odeven="odd";
			}

			$i++;
		  	
			$out.='<tr>';
			$out.='<td class="'.$odeven.'" >' . $row['fragger'] . ' </td>';
			$out.='<td class="'.$odeven.'">' . $row['frags'] .'</td>';
			$out.='</tr>';
			
		}
		
		$out.='</tbody>';
		$out.='</table>';
	}
	return $out;
}


/**************************************
*Player fragged
*
***************************************/
function getFraggeds($args) {

    $con = mysql_connect("localhost","barreirasacs","serverb");

	if (!$con)
	{
		$out="erro";
	}else{

		mysql_select_db("acscore", $con);
		
		$result = mysql_query("

SELECT fragged, count(fragged) as fraged FROM `gamefrag` WHERE 1
group by fragged
order by count(fragged) desc
limit 5

");

              $out='<table  border="1" cellpadding="2" cellspacing="2">';
		$out.='<thead>';
		$out.='<tr>';
		$out.='<th>PLAYER</th>';
		$out.='<th>FRAGGED</th>';
		$out.='</tr>';
		$out.='</thead>';
		$out.='<tbody>';

		$i=0;
		while($row = mysql_fetch_array($result))
		{
			if ($i % 2) {
				$odeven="even";
			} else {
				$odeven="odd";
			}

			$i++;
		  	
			$out.='<tr>';
			$out.='<td class="'.$odeven.'" >' . $row['fragged'] . ' </td>';
			$out.='<td class="'.$odeven.'">' . $row['fraged'] .'</td>';
			$out.='</tr>';
			
		}
		
		$out.='</tbody>';
		$out.='</table>';
	}
	return $out;
}


/**************************************
*Player fragged
*
***************************************/
function getFactor($args) {

    	$con = mysql_connect("localhost","root","root");

	if (!$con)
	{
		$out="erro";
	}else{

		mysql_select_db("acscore", $con);
		
		$result = mysql_query("

select fraggers.name as name,round((((frags+fragged)/4)*(frags/fragged)),2) as factor 

from 

(SELECT fragger as name, count(fragger) as frags FROM `gamefrag` WHERE 1
group by fragger) as fraggers

inner join 

(SELECT fragged as name, count(fragged) as fragged FROM `gamefrag` WHERE 1
group by fragged) as fraggeds

on   (fraggers.name=fraggeds.name)

order by (((frags+fragged)/4)*(frags/fragged)) desc

limit 5 
");

              $out='<table  border="1" cellpadding="2" cellspacing="2">';
		$out.='<thead>';
		$out.='<tr>';
		$out.='<th>PLAYER</th>';
		$out.='<th>RANKING</th>';
		$out.='</tr>';
		$out.='</thead>';
		$out.='<tbody>';

		$i=0;
		while($row = mysql_fetch_array($result))
		{
			if ($i % 2) {
				$odeven="even";
			} else {
				$odeven="odd";
			}

			$i++;
		  	
			$out.='<tr>';
			$out.='<td class="'.$odeven.'" >' . $row['name'] . ' </td>';
			$out.='<td class="'.$odeven.'">' . $row['factor'] .'</td>';
			$out.='</tr>';
			
		}
		
		$out.='</tbody>';
		$out.='</table>';
	}
	return $out;
}



/*******************************************/

function getTime($args) {
    return date('H:i:s dfsdfsdfsf');
}


/* Create the server and map the XML-RPC method names to the relevant functions */

$server = new IXR_Server(array(
    'bacs.getGames' => 'getGames',
    'bacs.getFraggers' => 'getFraggers',
    'bacs.getFraggeds' => 'getFraggeds',
	'bacs.getFactor' => 'getFactor',
    'test.getTime' => 'getTime'
));

?>
